package me.sauce.folkpocket48.base

import android.arch.lifecycle.LifecycleObserver
import android.os.Bundle
import android.support.annotation.IntDef
import android.support.annotation.LayoutRes
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.view_toolbar.*
import me.sauce.folkpocket48.R


/**
 * @author sauceWu .
 * @since 2017/12/20 17:20
 */

abstract class BaseActivity : AppCompatActivity() {
    companion object {
        @IntDef(NONE, DEFAULT, CUSTOM)
        @Retention(AnnotationRetention.SOURCE)
        annotation class ViewHolderStatus

        const val NONE = -1L
        const val DEFAULT = 0L
        const val CUSTOM = 1L
    }


    open val isSimpleToolbar = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val content: View = if (isSimpleToolbar) {
            LayoutInflater.from(this)
                    .inflate(R.layout.view_toolbar, window.decorView.rootView as ViewGroup, false)
                    .apply { findViewById<FrameLayout>(R.id.content_frame).addView(injectView()) }
        } else {
            injectView()
        }
        setContentView(content)

        setSupportActionBar(findViewById(R.id.toolbar))
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        initView()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // handle arrow click here
        if (item.itemId == android.R.id.home) {
            finish() // close this activity and return to preview activity (if there is any)
        }

        return super.onOptionsItemSelected(item)
    }

    abstract fun injectView(): View

    protected open fun initView() {}
    protected fun initRId(@LayoutRes rid: Int): View {
        return LayoutInflater.from(this).inflate(rid, content_frame, false)
    }

    fun addObservers(vararg observer: LifecycleObserver) {
        observer.forEach { lifecycle.addObserver(it) }
    }
}
